//
//  InputLimitHelper.m
//  
//  jhf
//
//  Created by Mac on 2019/10/25.
//  Copyright © 2019 Mac. All rights reserved.
//

#import "InputLimitHelper.h"
#import <CooperSDK/CooperSDK.h>

@implementation InputLimitHelper

+ (NSString *)limitTextInput:(UITextView *)tv conifg:(RowInputBaseM *)config; {
    NSString *resultStr = @"";
    NSString *allowStr = config.inputControl.allowStrings;
    if (!((allowStr && allowStr.length > 0) || config.inputControl.maxLength > 0 || config.inputControl.isMoney)) {
        config.value = tv.text;
        resultStr = config.value;
        return resultStr;
    }
    
    NSString *x = tv.text;
    if (x.length) {
        NSString *tempString = x;
        // 允许的字符串
        if (config.inputControl.allowStrings && allowStr.length > 0) {
            tempString = [[tempString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:config.inputControl.allowStrings] invertedSet]] componentsJoinedByString:@""];
        }
        // 判断最大长度
        if (config.inputControl.maxLength > 1) {
            if (tempString.length > config.inputControl.maxLength) {
                tempString = [tempString substringToIndex:config.inputControl.maxLength];
            }
        }
        // 判断金额
        if (config.inputControl.isMoney) {
            if (![tempString isMoney]) { // 不是金额 可能需要修正
                if (tempString.length > 0) {
                    if ([tempString hasSuffix:@"."]) { // 末尾是点
                        if (tempString.length == 1) { // 直接输入了.
                            tempString = @"0.";
                        }else if ([tempString countStr:@"."] != 1) { // 不止一个.
                            NSArray *array = [tempString componentsSeparatedByString:@"."];
                            tempString = [NSString stringWithFormat:@"%@.", array.firstObject];
                        }
                    }else if ([tempString countStr:@"."] == 1) {
                       NSArray *array = [tempString componentsSeparatedByString:@"."];
                       tempString = [NSString stringWithFormat:@"%@.%@", array.firstObject, [array.lastObject safeRetainSubstringToIndex:config.inputControl.moneyRightMaxLength]];
//                        tempString = [NSString stringWithFormat:@"%@.%@", array.firstObject, [array.lastObject safeRetainSubstringToIndex:2]];
                    }
                }
            }
        }
        // 修改已有内容
        if (![tempString isEqualToString:tv.text]) {
            UITextRange *selectRange = tv.selectedTextRange;
            tv.text = tempString;
            tv.selectedTextRange = selectRange;
            
            if ([@"0." isEqualToString:tempString]) { // 光标移动到最后
                UITextPosition *position = [tv endOfDocument];
                tv.selectedTextRange = [tv textRangeFromPosition:position toPosition:position];
            }
        }
        config.value = tempString;
    }else {
        config.value = @"";
    }
    resultStr = config.value;
    return resultStr;
}

+ (NSString *)limitInput:(UITextField *)tf conifg:(RowInputBaseM *)config; {
    NSString *resultStr = @"";
    NSString *allowStr = config.inputControl.allowStrings;
    if (!((allowStr && allowStr.length > 0) || config.inputControl.maxLength > 0 || config.inputControl.isMoney)) {
        config.value = tf.text;
        resultStr = config.value;
        return resultStr;
    }
    
    NSString *x = tf.text;
    if (x.length) {
        NSString *tempString = x;
        // 允许的字符串
        if (allowStr && allowStr.length > 0) {
            tempString = [[tempString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:allowStr] invertedSet]] componentsJoinedByString:@""];
        }
        // 判断最大长度
        if (config.inputControl.maxLength > 1) {
            if (tempString.length > config.inputControl.maxLength) {
                tempString = [tempString substringToIndex:config.inputControl.maxLength];
            }
        }
        // 判断金额
        if (config.inputControl.isMoney) {
            // 判断字符串左长度
            if ([tempString containsString:@"."] && config.inputControl.moneyLeftMaxLength > 0) {
                NSArray <NSString *>*arr = [tempString componentsSeparatedByString:@"."];
                if (arr[0].length > config.inputControl.moneyLeftMaxLength) {
                    tempString = [tempString substringToIndex:config.inputControl.moneyLeftMaxLength];
                    tempString = [NSString stringWithFormat:@"%@.%@", tempString, arr[1]];
                }
            }
            if (![tempString containsString:@"."] && config.inputControl.moneyLeftMaxLength > 0) {
                if (tempString.length > config.inputControl.moneyLeftMaxLength) {
                    tempString = [tempString substringToIndex:config.inputControl.moneyLeftMaxLength];
                }
            }
            if (![tempString isMoney]) { // 不是金额 可能需要修正
                if (tempString.length > 0) {
                    if ([tempString hasSuffix:@"."]) { // 末尾是点
                        if (tempString.length == 1) { // 直接输入了.
                            tempString = @"0.";
                        }else if ([tempString countStr:@"."] != 1) { // 不止一个.
                            NSArray *array = [tempString componentsSeparatedByString:@"."];
                            tempString = [NSString stringWithFormat:@"%@.", array.firstObject];
                        }
                    }else if ([tempString countStr:@"."] == 1) {
                       NSArray *array = [tempString componentsSeparatedByString:@"."];
                       tempString = [NSString stringWithFormat:@"%@.%@", array.firstObject, [array.lastObject safeRetainSubstringToIndex:config.inputControl.moneyRightMaxLength]];
//                       tempString = [NSString stringWithFormat:@"%@.%@", array.firstObject, [array.lastObject safeRetainSubstringToIndex:2]];
                    }
                }
            }
        }
        // 修改已有内容
        if (![tempString isEqualToString:tf.text]) {
            UITextRange *selectRange = tf.selectedTextRange;
            tf.text = tempString;
            tf.selectedTextRange = selectRange;
            if ([@"0." isEqualToString:tempString]) { // 光标移动到最后
                UITextPosition *position = [tf endOfDocument];
                tf.selectedTextRange = [tf textRangeFromPosition:position toPosition:position];
            }
            //            UITextPosition *beginning = tf.beginningOfDocument;
            //            NSRange range = [InputLimitHelper selectedRange:tf];
            //            UITextPosition *startPosition = [tf positionFromPosition:beginning offset:range.location - 1];
            //            UITextPosition *endPosition = [tf positionFromPosition:beginning offset:range.location - 1 + range.length];
            //            tf.text = tempString;
            //            // 创建一个UITextRange
            //            UITextRange *selectionRange = [tf textRangeFromPosition:startPosition toPosition:endPosition];
            //            tf.selectedTextRange = selectionRange;
        }
        config.value = tempString;
    }else {
        config.value = @"";
    }
    resultStr = config.value;
    return resultStr;
}

+ (NSRange)selectedRange:(UITextField *)tf {
    // 文首的位置
    UITextPosition *beginning = tf.beginningOfDocument;
    // 内容为[start,end)，无论是否有选取区域，start都描述了光标的位置
    UITextRange *selectedRange = tf.selectedTextRange;
    UITextPosition *selectionStart = selectedRange.start;
    UITextPosition *selectionEnd = selectedRange.end;
    // 获取以from为基准的to的偏移
    const NSInteger location = [tf offsetFromPosition:beginning toPosition:selectionStart];
    const NSInteger length = [tf offsetFromPosition:selectionStart toPosition:selectionEnd];
    return NSMakeRange(location, length);
}


@end
